package com.redhat.auth.log.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.redhat.auth.log.application.LogApplication;
import com.redhat.auth.log.core.domain.BizLogInfo;

public class LogAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private List<String> bizOperations;
	
	private List<LogNode> nodes;
	
	private String key;
	
	private String bizOperation;
	
	private String bizType;
	
	private List<String[]> aaData;
	
	private long logId;
	
	private String recordValue;
	
	private String content;
	
	private BizLogInfo bizLog;
	
	@Autowired
	private LogApplication logApplication;
	
	/**
	 * 进入日志管理查看的首页
	 * @return
	 */
	public String index(){
		bizOperations = logApplication.getBizOperations();
		return SUCCESS;
	}

	public String loadNode(){
		nodes = logApplication.getBizTypes(key);
		String json;
		try {
			json = JSONUtil.serialize(nodes);
	        WebUtil.writeJSON(ServletActionContext.getResponse(), json);
		} catch (JSONException e) {
		} 
		return null;
	}
	
	public String loadData(){
		List<BizLogInfo> bizLogInfos = logApplication.getBizLogInfo(bizOperation, bizType);
		aaData = getArrayBizLogInfos(bizLogInfos);
		return "json";
	}
	
	public String loadValue(){
	    bizLog = logApplication.getBizLogInfo(logId);
		if(bizLog==null)return "logNull";
		else if(bizLog.getResult().equals("SUCCESS")){
			content = logApplication.getLogDescription(getLanguage(), bizType,bizOperation, recordValue);
			return "logDesc";
		}else{
		   return "logError";	
		}
	}
	
	public String loadPage(){
		return "view";
	}

	public List<String> getBizOperations() {
		return bizOperations;
	}


	public void setBizOperations(List<String> bizOperations) {
		this.bizOperations = bizOperations;
	}

	public List<LogNode> getNodes() {
		return nodes;
	}

	public void setNodes(List<LogNode> nodes) {
		this.nodes = nodes;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getBizOperation() {
		return bizOperation;
	}

	public void setBizOperation(String bizOperation) {
		this.bizOperation = bizOperation;
	}

	public String getBizType() {
		return bizType;
	}

	public void setBizType(String bizType) {
		this.bizType = bizType;
	}

	public List<String[]> getAaData() {
		return aaData;
	}

	public void setAaData(List<String[]> aaData) {
		this.aaData = aaData;
	}

	public long getLogId() {
		return logId;
	}

	public void setLogId(long logId) {
		this.logId = logId;
	}

	private List<String[]> getArrayBizLogInfos(List<BizLogInfo> bizLogInfos){
		List<String[]> logs = new ArrayList<String[]>();

		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		for(BizLogInfo logInfo:bizLogInfos){
			String keyId = String.valueOf(logInfo.getId());
			String operation = logInfo.getBizOperation();
			String bizType = logInfo.getBizType();
			Date date = logInfo.getBizDate();
			String bizDate = dateFormater.format(date);
			String result = logInfo.getResult();
			String exception = logInfo.getException();
			String system = logInfo.getSystem();
			String operator = logInfo.getOperator();
			String serverIp = logInfo.getServerIp();
			String clientIp = logInfo.getClientIp();
			String recordValue = logInfo.getRecordValue();
			String[] log = new String[]{keyId,bizDate,result,operation,bizType,exception==null?"":exception,system,operator,serverIp,clientIp,recordValue==null?"":recordValue,"valueClick"};
			logs.add(log);
		}
		return logs;
	}

	public String getRecordValue() {
		return recordValue;
	}

	public void setRecordValue(String recordValue) {
		this.recordValue = recordValue;
	}
	
	
	
	
	public BizLogInfo getBizLog() {
		return bizLog;
	}

	public void setBizLog(BizLogInfo bizLog) {
		this.bizLog = bizLog;
	}

	private String getLanguage() {
		String language = null;
		//从session中取国际化值
		HttpServletRequest request = ServletActionContext.getRequest();
		language  = (String)request.getSession().getAttribute("IL18N-LANGUAGE");
		//从cookies中取国际化值
		if(language==null || "".equals(language)) language = getCookieLanguage(request);
		//cookies中取不到，取浏览器的值
		if(language==null || "".equals(language)) language = request.getLocale().getLanguage();
		return language;
	}
	
	/**
	 * 获取cookie中的国际化语言设置
	 * @param request
	 * @return
	 */
	private String getCookieLanguage(HttpServletRequest request){
		Cookie[] cookies= request.getCookies();
		if(cookies==null)return null;
		for(int i=0;i<cookies.length;i++){
			Cookie cookie =  cookies[i];
			if(cookie.getName().equals("IL18N-LANGUAGE")){
				return cookie.getValue();
			}
		}
		return null;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	

}
